package cn.fintecher.pangolin.service.dataimp.service;

import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.BeanUtils;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.Remark;
import cn.fintecher.pangolin.service.dataimp.elastic.RemarkElastic;
import cn.fintecher.pangolin.service.dataimp.model.WaitHandleListModel;
import cn.fintecher.pangolin.service.dataimp.model.request.AddRemarkRequest;
import cn.fintecher.pangolin.service.dataimp.model.response.CommentModel;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;


@Service("homeNoticeService")
public class HomeNoticeService {
    @Autowired
    private BaseService baseService;

    @Autowired
    private RemarkElastic remarkElastic;

    /**
     * 查询备忘录
     */
    public List<CommentModel> getComment(String token) {
        UserModel userModel = baseService.getUserByToken(token);
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        qb.must(matchPhraseQuery("operatorUserName.keyword", userModel.getUsername()));
        qb.must(rangeQuery("reminderTime").gte(ZWDateUtil.getNowDate().getTime()));
        List<Remark> remarks = IterableUtils.toList(remarkElastic.search(qb));
        Map<Date, CommentModel> map = new HashMap<>();
        remarks.forEach(remark -> {
            Date reminderDate = ZWDateUtil.getLocalTime(remark.getReminderTime());
            CommentModel model;
            if (map.containsKey(reminderDate)) {
                model = map.get(reminderDate);
            } else {
                model = new CommentModel();
                model.setReminderTime(reminderDate);
            }
            model.getResponses().add(remark);
            map.put(reminderDate, model);
        });
        return new ArrayList<>(map.values());
    }


    /**
     * 待处理事项
     */
    public WaitHandleListModel getWaitHandleList(String token) {
        return null;
    }

    /**
     * 新增公告
     */
    public void addRemark(AddRemarkRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        Remark remark = new Remark();
        BeanUtils.copyPropertiesIgnoreNull(request, remark);
        remark.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        remark.setOperatorName(userModel.getRealName());
        remark.setOperatorUserName(userModel.getUsername());
        remarkElastic.save(remark);
    }

    /**
     * 修改公告
     */
    public void modifyRemark(AddRemarkRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        Remark remark = remarkElastic.findById(request.getId()).orElseThrow(() -> new BadRequestException(null, "", "remark.is.null"));
        remark.setCommentContent(request.getCommentContent());
        remark.setReminderTime(request.getReminderTime());
        remark.setOperatorUserName(userModel.getUsername());
        remark.setOperatorName(userModel.getRealName());
        remarkElastic.save(remark);
    }

    /**
     * 删除备忘录
     */
    public void deleteRemark(String id) {
        remarkElastic.deleteById(id);
    }
}
